package com.atelier.plugin.provider

import com.atelier.plugin.projectWizard.util.ICONS
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder
import com.intellij.icons.AllIcons
import com.intellij.ide.util.DefaultPsiElementCellRenderer
import com.intellij.json.psi.JsonProperty
import com.intellij.openapi.editor.markup.GutterIconRenderer
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.search.FilenameIndex
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.PsiShortNamesCache
import com.intellij.psi.xml.XmlFile
import com.intellij.psi.xml.XmlTag
import javax.swing.Icon

class MyBatisJavaNavigationProvider : RelatedItemLineMarkerProvider()
{
    override fun collectNavigationMarkers(
        element: PsiElement, result: MutableCollection<in RelatedItemLineMarkerInfo<*>>
                                         )
    {
        if (element is PsiClass)
        {
            val psiClass: PsiClass = element
            println(psiClass.qualifiedName)
            val allMethods = element.allMethods
            if (psiClass.containingFile.virtualFile == null)
            {
                return
            }
            val targets: ArrayList<PsiElement> = ArrayList()

            // 1. 查找与 class 类名同名的 xml 文件, 并追加到结果集合
            val xmlFiles = FilenameIndex.getFilesByName(
                psiClass.project, psiClass.name + ".xml", GlobalSearchScope.projectScope(psiClass.project)
                                                       ).toList()
            for (xmlFile in xmlFiles)
            {
                if(xmlFile is XmlFile)
                {
                    var xml:XmlFile=xmlFile
                    println(xml.name)
                    val tag = xml.rootTag
                    if (tag != null)
                    {
                        println(tag.namespace)
                        if(tag.getAttributeValue("namespace") == psiClass.qualifiedName)
                        {
                            targets.add(tag)
                        }
                    }
                }
            }
            if (targets.isEmpty())
            {
                println("结果为空")
                return
            } // 构建导航样式
            val builder: NavigationGutterIconBuilder<PsiElement> =
                NavigationGutterIconBuilder.create(AllIcons.FileTypes.Xml).setTargets(targets)
                    .setTooltipText("Navigate to Xml resource").setAlignment(GutterIconRenderer.Alignment.RIGHT)
                    .setCellRenderer(MyMapperCellRenderer())
            if (null != psiClass.nameIdentifier)
            {
                result.add(builder.createLineMarkerInfo(psiClass.nameIdentifier!!))
            }
            if(!targets.isNullOrEmpty())
            {

                for (method in allMethods)
                {
                    val methodTargets: ArrayList<PsiElement> = ArrayList()
                    val psiElement = targets.get(0)
                    if( psiElement is XmlTag)
                    {
                       var xmlFile:XmlTag= psiElement
                        val findXmlTagById = findXmlTagById(xmlFile, method.name)

                        if (findXmlTagById != null)
                        {
                            methodTargets.add(findXmlTagById)
                            val methodBuilder =
                                NavigationGutterIconBuilder.create(AllIcons.FileTypes.Xml).setTargets(methodTargets)
                                    .setTooltipText("Navigate to Xml resource").setAlignment(GutterIconRenderer.Alignment.RIGHT)
                                    .setCellRenderer(MyMapperCellRenderer())
                            result.add(methodBuilder.createLineMarkerInfo(method.nameIdentifier!!))
                        }
                    }

                }
            }
        }



    }
    fun findXmlTagById(xmlTag: XmlTag, tagId: String): XmlTag? {
        // 查找当前标签的 ID 属性
        val idAttribute = xmlTag.getAttribute("id")
        if (idAttribute != null && idAttribute.value == tagId) {
            return xmlTag
        }

        // 递归查找子标签
        for (childTag in xmlTag.subTags) {
            val foundTag = findXmlTagById(childTag, tagId)
            if (foundTag != null) {
                return foundTag
            }
        }

        return null
    }
    class MyMapperCellRenderer : DefaultPsiElementCellRenderer()
    {
        override fun getElementText(element: PsiElement): String
        {
            val prefix = StringBuilder()
            if (element is JsonProperty)
            {
                prefix.append("Found XML! ")
            }
            return prefix.append(super.getElementText(element)).toString()
        }

        override fun getIcon(element: PsiElement): Icon
        {
            return AllIcons.FileTypes.Xml
        }
    }
}







